/*!
 * @file Joystick.cpp
 */
#include "StdAfx.h"
#include <dinput.h>
#include "Input/src/JoystickImpl.h"
#include "Input/src/ManagerImpl.h"

namespace NTEngine
{
namespace Input
{

BOOL CALLBACK Joystick::Impl::EnumObjectsCallBack(const DIDEVICEOBJECTINSTANCE* object, void* data)
{
	if (object->dwType & DIDFT_AXIS)
	{
		DIPROPRANGE prop;
		prop.diph.dwSize = sizeof(DIPROPRANGE);
		prop.diph.dwHeaderSize = sizeof(DIPROPHEADER);
		prop.diph.dwObj = object->dwType;
		prop.diph.dwHow = DIPH_BYID;
		prop.lMin = -JOYAXIS_RANGE;
		prop.lMax = +JOYAXIS_RANGE;

		IDirectInputDevice8* device = static_cast<IDirectInputDevice8*>(data);
		HRESULT hr = device->SetProperty(DIPROP_RANGE, &prop.diph);
		if (FAILED(hr))
		{
			return DIENUM_STOP;
		}
	}

	return DIENUM_CONTINUE;
}

bool Joystick::isOn(JoyButton b) const
{
	ASSERT(m_Impl);
	return m_Impl->isOn(b);
}

bool Joystick::isTriggered(JoyButton b) const
{
	ASSERT(m_Impl);
	return m_Impl->isTriggered(b);
}

float Joystick::axis(Stick s) const
{
	ASSERT(m_Impl);
	return m_Impl->axis(s);
}

bool Joystick::isEnabled() const
{
	ASSERT(m_Impl);
	return m_Impl->isEnabled();
}

Joystick::Joystick(Impl* impl)
: m_Impl(impl)
{
}

} // namespace Input
} // namespace NTEngine
// EOF